package com.corhyfar.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.corhyfar.beans.ClienteDTO;
import com.corhyfar.beans.ClienteTemporalDTO;
import com.corhyfar.dao.ClienteTemporalDAO;
import com.corhyfar.util.SQLConnection;

public class ClienteTemporalDaoImpl implements ClienteTemporalDAO {

	Connection cn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	@Override
	public ClienteTemporalDTO obtenerClienteTemporal(int codigoClienteTemp)
			throws SQLException {
		ClienteTemporalDTO obtenido = null;
		cn = new SQLConnection().GetConnection();
		String sql = "select * from CLIENTE_TEMP where CODCLI_TEMP=?";
		ps = cn.prepareStatement(sql);
		ps.setInt(1, codigoClienteTemp);
		rs = ps.executeQuery();
		if (rs.next()) {
			obtenido = new ClienteTemporalDTO(rs.getString(1), rs.getString(2),
					rs.getString(3), rs.getString(4), rs.getString(5),
					rs.getString(6), rs.getString(7));

		}
		ps.close();
		cn.close();
		return obtenido;
	}

	@Override
	public List<ClienteTemporalDTO> listarClienteTemporal() {

		List<ClienteTemporalDTO> lista = new ArrayList<ClienteTemporalDTO>();
		cn = SQLConnection.GetConnection();
		String sql = "select * from CLIENTE_TEMP";
		try {
			ps = cn.prepareStatement(sql);

			rs = ps.executeQuery();
			while (rs.next()) {

				lista.add(new ClienteTemporalDTO(rs.getString(1), rs
						.getString(2), rs.getString(3), rs.getString(4), rs
						.getString(5), rs.getString(6), rs.getString(7)));

			}
			ps.close();
			cn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return lista;
	}

	@Override
	public void actualizaTrabajadores(String nombreClienteTemp,
			String apellidoPaternoClienteTemp,
			String apellidoMaternoClienteTemp, String telefonoClienteTemp,
			String emailClienteTemp, String empresaClienteTemp,
			String mensajeClienteTemp) throws SQLException {

		cn = new SQLConnection().GetConnection();
		String sql = "update CLIENTE_TEMP set NOMCLI_TEMP=?, APE_PATCLI_TEMP=? ,APE_MATCLI_TEMP =?,TELEF_CELCLI_TEMP =?,EMAILCLI =?,EMPRESACLI_TEMP =?,MENSAJECLI_TEMP=?";
		ps = cn.prepareStatement(sql);
		ps.setString(1, nombreClienteTemp);
		ps.setString(2, apellidoMaternoClienteTemp);
		ps.setString(3, apellidoPaternoClienteTemp);
		ps.setString(4, telefonoClienteTemp);
		ps.setString(5, emailClienteTemp);
		ps.setString(6, mensajeClienteTemp);
		int rst = ps.executeUpdate();

		if (rst > 0) {
			System.out.println("Cliente Modificado");
		}
		ps.close();
		cn.close();

	}

	@Override
	public void ingresaClienteTemporal(ClienteTemporalDTO clienteTemporalDTO) {

		try {
			cn = new SQLConnection().GetConnection();
			String sql = "insert into CLIENTE_TEMP (NOMCLI_TEMP,APE_PATCLI_TEMP ,APE_MATCLI_TEMP ,TELEF_CELCLI_TEMP ,EMAILCLI_TEMP ,EMPRESACLI_TEMP ,MENSAJECLI_TEMP) values (?,?,?,?,?,?,?)";
			ps = cn.prepareStatement(sql);

			ps.setString(1, clienteTemporalDTO.getNombreClienteTemp());
			ps.setString(2, clienteTemporalDTO.getApellidoPaternoClienteTemp());
			ps.setString(3, clienteTemporalDTO.getApellidoMaternoClienteTemp());
			ps.setString(4, clienteTemporalDTO.getTelefonoClienteTemp());
			ps.setString(6, clienteTemporalDTO.getEmailClienteTemp());
			ps.setString(5, clienteTemporalDTO.getEmpresaClienteTemp());
			ps.setString(7, clienteTemporalDTO.getMensajeClienteTemp());

			int rst = ps.executeUpdate();
			ps.close();
			cn.close();

			System.out.println("Cliente temporal registrado");

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void eliminaClienteTemporal(int codigoClienteTemp) {
		// TODO Auto-generated method stub

		try {
			cn = new SQLConnection().GetConnection();
			String sql = "delete from CLIENTE_TEMP where CODCLI_TEMP=?";
			ps = cn.prepareStatement(sql);
			ps.setInt(1, codigoClienteTemp);
			rs = ps.executeQuery();
			ps.close();
			cn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
